package com.demos.springcloud.consumer04HystrixBreak.controller;

import com.demos.springcloud.consumer04HystrixBreak.service.ExceptionProviderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("break")
public class controller {

    @Autowired
    private ExceptionProviderService exceptionProviderService;

    @GetMapping("expost")
    @HystrixCommand(fallbackMethod = "method1Fallback", commandProperties = {
            //以下参数在 HystrixCommandProperties 类中有默认配置
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否开启熔断器
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000"), //统计时间窗
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), //统计时间窗内请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "60000"), //休眠时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") //在统计时间窗口期以内，请求失败率达到 60% 时进入熔断状态
    })
    public String method1() {
        System.out.println("---------- method1 ------------");
        return exceptionProviderService.expPost("break").toString();
    }

    public String method1Fallback() {
        System.out.println("---------- method1降级 ------------");
        return "method1降级";
    }
}
